#!/usr/bin/python

import examples.basegame
import game.entity
import game.gameworld
import ai.steer.engine as steer
import euclid
import random
import events.dispatcher
import pygame

class SeekBoid(examples.basegame.DemoBoid):
    def __init__(self, window, pos, mass, maxspeed, maxforce, maxturnrate):
        super( SeekBoid, self ).__init__( window, pos, mass, maxspeed, maxforce, maxturnrate )

        target = euclid.Vector3(
            random.uniform( 0, self.window.width ),
            random.uniform( 0, self.window.height ),
            0 )
        self.steer.add( steer.Seek( target ) )


class SeekDemo( examples.basegame.BaseGame ):
    def setup(self):
        super(SeekDemo, self).setup()
        pos =  euclid.Vector3(
            random.uniform( 0, self.window.width ),
            random.uniform( 0, self.window.height ),
            0 )

        self.boid = SeekBoid( self.window, pos, 10, 50, 50, 50 )
        self.mark = None

        events.dispatcher.instance().register( events.dispatcher.PyGameMouseButtonUp, self._onMouse )

    def _onKey( self, event ):
        if event.key == pygame.K_q: self.boid.mass += 1
        elif event.key == pygame.K_a:
            self.boid.mass -= 1
            self.boid.mass = max( 1, self.boid.mass )
        elif event.key == pygame.K_w: self.boid.maxForce += 1
        elif event.key == pygame.K_s:
            self.boid.maxForce -= 1
            self.boid.maxForce = max( 1, self.boid.maxForce )
        elif event.key == pygame.K_e: self.boid.maxSpeed += 1
        elif event.key == pygame.K_d:
            self.boid.maxSpeed -= 1
            self.boid.maxSpeed = max( 1, self.boid.maxSpeed )
        elif event.key == pygame.K_r: self.boid.maxTurnRate += 1
        elif event.key == pygame.K_f:
            self.boid.maxTurnRate -= 1
            self.boid.maxTurnRate = max( 1, self.boid.maxTurnRate )
        else:
             super(SeekDemo, self)._onKey(event)


    def _onMouse(self, event):
        if self.MOUSE_LEFT == event.button:
            if self.mark:
                game.gameworld.instance().remove( self.mark )
            self.mark = examples.basegame.Point( self.window, euclid.Vector3( event.pos[0], event.pos[1], 0 ) )
            #Set a new seek position at the click point
            self.boid.steer.add( steer.Seek( euclid.Vector3( event.pos[0], event.pos[1], 0 ) ) )

